package demo.xlj.yuyi.studentapp.dudaoyuan;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.KeyEvent;
import android.view.View;
import android.view.animation.AnimationUtils;
import android.widget.ImageSwitcher;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.ViewSwitcher;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import demo.xlj.yuyi.studentapp.R;
import demo.xlj.yuyi.studentapp.base.BaseRxActivity;
import demo.xlj.yuyi.studentapp.common.CommunityStyleActivity;
import demo.xlj.yuyi.studentapp.common.FuZhongStyleActivity;
import demo.xlj.yuyi.studentapp.common.LookAnnouncementActivity;
import demo.xlj.yuyi.studentapp.common.MyCommicationActivity;
import demo.xlj.yuyi.studentapp.common.SendTongZhiChooseActivity;
import demo.xlj.yuyi.studentapp.common.SocialActivity;
import demo.xlj.yuyi.studentapp.common.StudentStyleActivity;
import demo.xlj.yuyi.studentapp.common.WhereSchoolActivity;
import demo.xlj.yuyi.studentapp.common.YouthLeagueNewsActivity;
import demo.xlj.yuyi.studentapp.login.StudentMeetLoginActivity;
import demo.xlj.yuyi.studentapp.shetuanshencha.AllowOrNotHuoDongActivity;
import demo.xlj.yuyi.studentapp.utils.ActivityUtil;
import demo.xlj.yuyi.studentapp.utils.SharePerferencesUtils;
import demo.xlj.yuyi.studentapp.utils.ToastUtil;

/***
 * 督导员Activity
 */
public class DuDaoYuanActivity extends BaseRxActivity {


    @BindView(R.id.ImageView_Du_Dao_Yuan_Community_Style)
    ImageView mImageViewDuDaoYuanCommunityStyle;
    @BindView(R.id.ImageView_Du_Dao_Yuan_Fu_Zhong_Style)
    ImageView mImageViewDuDaoYuanFuZhongStyle;
    @BindView(R.id.ImageView_Du_Dao_Yuan_Youth_League_news)
    ImageView mImageViewDuDaoYuanYouthLeagueNews;
    @BindView(R.id.ImageView_Du_Dao_Yuan_social_activity)
    ImageView mImageViewDuDaoYuanSocialActivity;
    @BindView(R.id.ImageView_Du_Dao_Yuan_Student_Style)
    ImageView mImageViewDuDaoYuanStudentStyle;
    @BindView(R.id.ImageView_Du_Dao_Yuan_My_Commication)
    ImageView mImageViewDuDaoYuanMyCommication;
    @BindView(R.id.ImageView_Du_Dao_Yuan_Look_Announcement)
    ImageView mImageViewDuDaoYuanLookAnnouncement;
    @BindView(R.id.ImageView_Du_Dao_Yuan_Announcement_Gong_Shi)
    ImageView mImageViewDuDaoYuanAnnouncementGongShi;
    @BindView(R.id.ImageView_Du_Dao_Yuan_Fa_Bu_Announcement)
    ImageView mImageViewDuDaoYuanFaBuAnnouncement;
    @BindView(R.id.ImageSwitcher_dudao)
    ImageSwitcher mImageSwitcherDudao;
    @BindView(R.id.TextView_dudao_huanying)
    TextView mTextViewDudaoHuanying;

    private int[] images = {R.drawable.lunbo4, R.drawable.lunbo5, R.drawable.lunbo6,R.drawable.lunbo7};//图片资源数组
    private int position = 0;

    private Handler myHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 11:
                    position++;
                    mImageSwitcherDudao.setImageResource(images[position]);
                    if (position == 3) {
                        position = -1;
                    }
                    myHandler.sendEmptyMessageDelayed(11, 4000);
                    break;
            }
        }
    };

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_du_dao_yuan);
        ButterKnife.bind(this);
        String name = (String) SharePerferencesUtils.get(DuDaoYuanActivity.this, "name", "", 0);
        ToastUtil.success(DuDaoYuanActivity.this, "欢迎" + name + "归来！", true, true);
        mTextViewDudaoHuanying.setText("欢迎您 督导员"+name+"用户");
        initImageView();

    }

    //初始化，上面的轮播图
    private void initImageView() {
        mImageSwitcherDudao.setFactory(new ViewSwitcher.ViewFactory() {//设定工厂，每进来一个图片都用一个ImageView接收
            @Override
            public View makeView() {
                ImageView imageView = new ImageView(DuDaoYuanActivity.this);
                imageView.setScaleType(ImageView.ScaleType.FIT_XY);
                return imageView;
            }
        });//这个设置Factory是必须的
        mImageSwitcherDudao.setImageResource(images[position]);//设置初始图片,也是通过这个代码设置在控件上显示的图片
        mImageSwitcherDudao.setInAnimation(AnimationUtils.loadAnimation(this, R.anim.lunbo_in));
        mImageSwitcherDudao.setOutAnimation(AnimationUtils.loadAnimation(this, R.anim.lunbo_out));//动画淡出

        myHandler.sendEmptyMessageDelayed(11, 3000);

        mImageSwitcherDudao.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //点击事件
                ActivityUtil.openActivityFromLeftToRight(DuDaoYuanActivity.this, WhereSchoolActivity.class);
            }
        });
    }

    @OnClick({R.id.ImageView_Du_Dao_Yuan_Community_Style, R.id.ImageView_Du_Dao_Yuan_Fu_Zhong_Style, R.id.ImageView_Du_Dao_Yuan_Youth_League_news, R.id.ImageView_Du_Dao_Yuan_social_activity, R.id.ImageView_Du_Dao_Yuan_Student_Style, R.id.ImageView_Du_Dao_Yuan_My_Commication, R.id.ImageView_Du_Dao_Yuan_Look_Announcement, R.id.ImageView_Du_Dao_Yuan_Announcement_Gong_Shi, R.id.ImageView_Du_Dao_Yuan_Fa_Bu_Announcement})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.ImageView_Du_Dao_Yuan_Community_Style:
                //社团风貌
                ActivityUtil.openActivityFromLeftToRightByResult(DuDaoYuanActivity.this, CommunityStyleActivity.class,"type","0");
                break;
            case R.id.ImageView_Du_Dao_Yuan_Fu_Zhong_Style:
                //附中风采
                ActivityUtil.openActivityFromLeftToRight(DuDaoYuanActivity.this, FuZhongStyleActivity.class);
                break;
            case R.id.ImageView_Du_Dao_Yuan_Youth_League_news:
                //附中要闻
                ActivityUtil.openActivityFromLeftToRight(DuDaoYuanActivity.this, YouthLeagueNewsActivity.class);
                break;
            case R.id.ImageView_Du_Dao_Yuan_social_activity:
                //社团活动
                ActivityUtil.openActivityFromLeftToRight(DuDaoYuanActivity.this, SocialActivity.class);
                break;
            case R.id.ImageView_Du_Dao_Yuan_Student_Style:
                //学生会风采
                ActivityUtil.openActivityFromLeftToRight(DuDaoYuanActivity.this, StudentStyleActivity.class);
                break;
            case R.id.ImageView_Du_Dao_Yuan_My_Commication:
                //我的
                ActivityUtil.openActivityForResultFromLeftToRight(DuDaoYuanActivity.this, MyCommicationActivity.class, 99);
                break;
            case R.id.ImageView_Du_Dao_Yuan_Look_Announcement:
                //公告通知
                ActivityUtil.openActivityFromLeftToRight(DuDaoYuanActivity.this, LookAnnouncementActivity.class);
                break;
            case R.id.ImageView_Du_Dao_Yuan_Announcement_Gong_Shi:
                //待审核
                ActivityUtil.openActivityFromLeftToRight(DuDaoYuanActivity.this, AllowOrNotHuoDongActivity.class);
                break;
            case R.id.ImageView_Du_Dao_Yuan_Fa_Bu_Announcement:
                //发布通知
//                ActivityUtil.openActivityFromLeftToRight(DuDaoYuanActivity.this, FaBuAnnouncementActivity.class);
                ActivityUtil.openActivityFromLeftToRight(DuDaoYuanActivity.this, SendTongZhiChooseActivity.class);
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 99&&resultCode == 88) {
            ActivityUtil.openActivityFromLeftToRight(DuDaoYuanActivity.this, StudentMeetLoginActivity.class);
            DuDaoYuanActivity.this.finish();
        }
    }


    private long exitTime = 0;

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event)
    {
        if(keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_DOWN)
        {

            if((System.currentTimeMillis()-exitTime) > 2000)  //System.currentTimeMillis()无论何时调用，肯定大于2000
            {
                Toast.makeText(getApplicationContext(), "再按一次退出程序",Toast.LENGTH_SHORT).show();
                exitTime = System.currentTimeMillis();
            }else
            {
                finish();
                System.exit(0);
            }
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

}
